package com.wzt.lianfirecontrol.adapter.xuncha;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.function.xuncha.XunChaFloorModel;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.Utils;

public class XunChaSelectFloorAdapter extends BaseRecyclerAdapter<XunChaFloorModel> {

    private Context context;

    private int listPosition;

    private boolean isHistory = false;

    private boolean isComplete = false;

    public boolean isComplete() {
        return isComplete;
    }

    public boolean isHistory() {
        return isHistory;
    }

    public void setHistory(boolean history) {
        isHistory = history;
    }

    public int getListPosition() {
        return listPosition;
    }

    public void setListPosition(int listPosition) {
        this.listPosition = listPosition;
    }


    public void setComplete(boolean complete) {
        isComplete = complete;
    }

    public XunChaSelectFloorAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_xuncha_select_floor, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, XunChaFloorModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{

        private LinearLayout ll_item_content;
        private TextView tv_floor_name;
        private TextView tv_name;
        private TextView tv_status;
        private ImageView iv_floor_select;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.tv_floor_name = itemView.findViewById(R.id.tv_floor_name);
        myViewHolder.tv_name = itemView.findViewById(R.id.tv_name);
        myViewHolder.tv_status = itemView.findViewById(R.id.tv_status);
        myViewHolder.iv_floor_select = itemView.findViewById(R.id.iv_floor_select);
    }

    private void setItemData(MyViewHolder viewHolder, XunChaFloorModel data, int realPosition) {
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.ll_item_content.getLayoutParams();
        if (realPosition == 0) {
            layoutParams.setMargins(0, 0, 0, 0);
        } else {
            layoutParams.setMargins(0, Utils.dip2px(context, 0.5f), 0, 0);
        }

        if (isHistory) {
            viewHolder.ll_item_content.setBackgroundColor(context.getResources().getColor(R.color.white));
        } else {
            viewHolder.ll_item_content.setBackgroundColor(context.getResources().getColor(R.color.divider_line_color));
        }

       viewHolder.tv_floor_name.setText(data.getFloorName());
       if (isComplete){
           viewHolder.tv_floor_name.setTextColor(context.getResources().getColor(R.color.hint_text_color));
           if (StringUtils.isEmpty(data.getRealName())){
               viewHolder.tv_name.setVisibility(View.INVISIBLE);
           }else {
               viewHolder.tv_name.setVisibility(View.VISIBLE);
               viewHolder.tv_name.setText(data.getRealName());
           }
           viewHolder.tv_status.setVisibility(View.VISIBLE);
           // 防火巡查
           switch (data.getStatus()) {
               case "0":
                   viewHolder.tv_name.setVisibility(View.INVISIBLE);
                   viewHolder.iv_floor_select.setVisibility(View.GONE);
                   viewHolder.tv_status.setVisibility(View.VISIBLE);
                   viewHolder.tv_status.setText("待检查");
                   viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.change_status_color));
                   break;
               case "1":
                   if (StringUtils.isEmpty(data.getRealName())) {
                       viewHolder.tv_name.setVisibility(View.INVISIBLE);
                   } else {
                       viewHolder.tv_name.setVisibility(View.VISIBLE);
                       viewHolder.tv_name.setText(data.getRealName());
                   }
                   viewHolder.iv_floor_select.setVisibility(View.GONE);
                   viewHolder.tv_status.setVisibility(View.VISIBLE);
                   viewHolder.tv_status.setText("检查中");
                   viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.look_status_color));
                   break;
               case "2":
                   if (StringUtils.isEmpty(data.getRealName())) {
                       viewHolder.tv_name.setVisibility(View.INVISIBLE);
                   } else {
                       viewHolder.tv_name.setVisibility(View.VISIBLE);
                       viewHolder.tv_name.setText(data.getRealName());
                   }
                   viewHolder.iv_floor_select.setVisibility(View.GONE);
                   viewHolder.tv_status.setVisibility(View.VISIBLE);
                   viewHolder.tv_status.setText("已完成");
                   viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.main_color));
                   break;
               case "3":
                   viewHolder.tv_name.setVisibility(View.INVISIBLE);
                   viewHolder.iv_floor_select.setVisibility(View.GONE);
                   viewHolder.tv_status.setVisibility(View.VISIBLE);
                   viewHolder.tv_status.setText("已过期");
                   viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.hint_text_color));
                   break;
           }
           viewHolder.iv_floor_select.setVisibility(View.GONE);
       }else {
           viewHolder.tv_floor_name.setTextColor(context.getResources().getColor(R.color.text_color));
           viewHolder.tv_name.setVisibility(View.INVISIBLE);
           viewHolder.iv_floor_select.setVisibility(View.GONE);
           if ("1".equals(data.getIsAllSelect())) {
               // 防火巡查
               switch (data.getStatus()) {
                   case "0":
                   case "1":
                   case "3":
                       viewHolder.iv_floor_select.setVisibility(View.VISIBLE);
                       if ("0".equals(data.getCurrentSelectStatus())) {
                           viewHolder.iv_floor_select.setImageResource(R.mipmap.icon_single_unselect);
                       } else {
                           viewHolder.iv_floor_select.setImageResource(R.mipmap.icon_single_select);
                       }
                       break;
                   case "2":
                       if (StringUtils.isEmpty(data.getRealName())) {
                           viewHolder.tv_name.setVisibility(View.INVISIBLE);
                       } else {
                           viewHolder.tv_name.setVisibility(View.VISIBLE);
                           viewHolder.tv_name.setText(data.getRealName());
                       }
                       viewHolder.iv_floor_select.setVisibility(View.GONE);
                       break;
               }

               // 防火巡查
               switch (data.getStatus()) {
                   case "0":
                       viewHolder.tv_status.setVisibility(View.VISIBLE);
                       viewHolder.tv_status.setText("待检查");
                       viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.change_status_color));
                       break;
                   case "1":
                       viewHolder.tv_status.setVisibility(View.VISIBLE);
                       viewHolder.tv_status.setText("检查中");
                       viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.look_status_color));
                       break;
                   case "2":
                       viewHolder.tv_status.setVisibility(View.VISIBLE);
                       viewHolder.tv_status.setText("已完成");
                       viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.main_color));
                       break;
                   case "3":
                       viewHolder.tv_status.setVisibility(View.VISIBLE);
                       viewHolder.tv_status.setText("已过期");
                       viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.hint_text_color));
                       break;
                   default:
                       viewHolder.tv_status.setVisibility(View.VISIBLE);
                       break;
               }
           }else {
               viewHolder.tv_status.setVisibility(View.GONE);
               viewHolder.iv_floor_select.setVisibility(View.VISIBLE);
               if ("0".equals(data.getCurrentSelectStatus())) {
                   viewHolder.iv_floor_select.setImageResource(R.mipmap.icon_single_unselect);
               } else {
                   viewHolder.iv_floor_select.setImageResource(R.mipmap.icon_single_select);
               }
           }
       }

    }
}
